package com.addplus.townmall.server.web.provider.utils;

import com.addplus.townmall.server.api.constant.StringConstant;
import com.addplus.townmall.server.connector.redis.RedisSlaveTemplateManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * @author fuyq
 * @date 2018/10/26
 */
@Component
public class ShopProductUtils {

    @Autowired
    private RedisSlaveTemplateManager redisSlaveTemplateManager;

    /**
     * 1-商品 2-礼品卡
     *
     * @param type
     * @return
     */
    public String getShopProductCode(Integer type) {
        RedisTemplate redisTemplate = redisSlaveTemplateManager.getMasterTemplate();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Long code = 0L;
        String key = null;
        long value = 0;
        if (type == 1) {
            key = StringConstant.SHOP_PRODUCT_CODE_KEY;
            value = 10000;
        } else if (type == 2) {
            key = StringConstant.GIFT_CODE_KEY;
            value =100;
        }
        if (redisTemplate.hasKey(key)) {
            code = valueOperations.increment(key, 1);
        } else {
            code = valueOperations.increment(key, value);
        }
        return String.valueOf(code);
    }

}
